package org.helyx.gae.getabike.registry;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

public class DefaultRegistry<U, V> implements Registry<U, V>, InitializingBean {

	private boolean initialized = false;

	private Map<U, V> registryMap = new HashMap<U, V>();

	public DefaultRegistry() {
		super();
	}

	public void setRegistryMap(Map<U, V> registryMap) {
		checkInitialized();
		this.registryMap = registryMap;
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		initialized = true;
		Assert.notNull(registryMap, "[Assertion failed] - registryMap is required; it must not be null");
	}

	public void put(U u, V v) {
		checkInitialized();
		this.registryMap.put(u, v);
	}

	private void checkInitialized() {
		if (initialized) {
			throw new IllegalStateException("Registry is already configured");
		}
	}

	@Override
	public boolean contains(U u) {
		boolean contains = registryMap.containsKey(u);
		
		return contains;
	}

	@Override
	public V get(U u) {
		V v = registryMap.get(u);
		
		return v;
	}

}
